package com.iraeta.controller.kcpd;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.Config;
import com.iraeta.common.Result;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.main.ErpCsSnapshot;
import com.iraeta.entity.main.ErpCsSnapshotRecord;
import com.iraeta.entity.main.vo.ErpCurrentStoreVo;
import com.iraeta.service.basic.CsSnapshotService;
import com.iraeta.uitl.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController(value = "CsSnapshotController")
@RequestMapping("cssnapshot")
public class CsSnapshotController extends BaseController {
    @Autowired
    CsSnapshotService csSnapshotService;
    @Autowired
    Config config;

    @RequestMapping("createcssnapshot")
    public String createCsSnapshot(@RequestBody JSONObject jo){
        String title = jo.getString("title");
        Integer planId = jo.getIntValue("planId");
        JSONArray selectedWhcodes = jo.getJSONArray("selectedWhcodes");
        Integer nowMonth = jo.getInteger("nowMonth");
        ErpCsSnapshot erpCsSnapshot = new ErpCsSnapshot();
        erpCsSnapshot.setPlanid(planId);
        erpCsSnapshot.setTitle(title);
        erpCsSnapshot.setCreateuser(this.getUserName());
        erpCsSnapshot.setCreatetime(new Date());
        csSnapshotService.createCsSnapshot(erpCsSnapshot, selectedWhcodes, nowMonth);
        return this.success();
    }

    /*全部盘点记录查看*/
    @RequestMapping("listcssnapshot")
    public String listCsSnapshot(@RequestBody JSONObject jo){
        String title = jo.getString("title");
        String createUser = jo.getString("createUser");
        this.pageSet(jo);
        List<ErpCsSnapshot> erpCsSnapshotList = csSnapshotService.listErpCsSnapshot(title, createUser);
        PageInfo<ErpCsSnapshot> erpCsSnapshotListPageInfo = this.getPageInfo(erpCsSnapshotList);
        return JSON.toJSONString(Result.success(erpCsSnapshotListPageInfo));
    }

    /*自己发布的盘点记录查看*/
    @RequestMapping("listcssnapshotself")
    public String listCsSnapshotSelf(@RequestBody JSONObject jo){
        String title = jo.getString("title");
        this.pageSet(jo);
        List<ErpCsSnapshot> erpCsSnapshotList = csSnapshotService.listErpCsSnapshotSelf(title, this.getUserName());
        PageInfo<ErpCsSnapshot> erpCsSnapshotListPageInfo = this.getPageInfo(erpCsSnapshotList);
        return JSON.toJSONString(Result.success(erpCsSnapshotListPageInfo));
    }

    /*计划下的盘点记录查看*/
    @RequestMapping("listcssnapshotplan")
    public String listCsSnapshotPlan(@RequestBody JSONObject jo){
        Integer planId = jo.getInteger("planId");
        String createUser = jo.getString("createUser");
        String startDate = jo.getString("startDate");
        String endDate = StrUtil.isEmpty(jo.getString("endDate")) ? null : DateUtil.formatDate(DateUtil.offsetDay( DateUtil.parseDate( jo.getString("endDate")), 1)) ;
        this.pageSet(jo);
        List<ErpCsSnapshot> erpCsSnapshotList = csSnapshotService.listErpCsSnapshotPlan(planId, createUser, startDate, endDate);
        PageInfo<ErpCsSnapshot> erpCsSnapshotListPageInfo = this.getPageInfo(erpCsSnapshotList);
        return JSON.toJSONString(Result.success(erpCsSnapshotListPageInfo));
    }

    /*查看某个盘点记录的盘点数据*/
    @RequestMapping("listcssnapshotrecord")
    public String listCsSnapshotRecord(@RequestBody JSONObject jo){
        Integer ssId = jo.getInteger("ssId");
        String cwhname = jo.getString("cwhname");
        String cinvcode = jo.getString("cinvcode");
        String cinvname = jo.getString("cinvname");
        Integer iquantityflag = (jo.getBoolean("iquantityflag") == null || !jo.getBoolean("iquantityflag"))  ? null : 1;
        Integer pdflag = (jo.getBoolean("pdflag") == null || !jo.getBoolean("pdflag"))  ? null : 1;
        Integer czquantityflag = (jo.getBoolean("czquantityflag") == null || !jo.getBoolean("czquantityflag"))  ? null : 1;
        Integer npdflag = (jo.getBoolean("npdflag") == null || !jo.getBoolean("npdflag"))  ? null : 1;
        ErpCurrentStoreVo erpCurrentStoreVo = new ErpCurrentStoreVo(cwhname, cinvcode, cinvname);
        erpCurrentStoreVo.setSsid(ssId);
        erpCurrentStoreVo.setIquantityflag(iquantityflag);
        erpCurrentStoreVo.setPdflag(pdflag);
        erpCurrentStoreVo.setNpdflag(npdflag);
        erpCurrentStoreVo.setCzquantityflag(czquantityflag);
        this.pageSet(jo);
        List<ErpCsSnapshotRecord> erpCsSnapshotRecordList = csSnapshotService.listErpCsSnapshotRecord(erpCurrentStoreVo);
        PageInfo<ErpCsSnapshotRecord> erpCsSnapshotRecordPageInfo = this.getPageInfo(erpCsSnapshotRecordList);
        return JSON.toJSONString(Result.success(erpCsSnapshotRecordPageInfo));
    }

    /*导出现存量的盘点数据*/
    @RequestMapping("exportcssnapshotrecord")
    public String exportPdNow(@RequestBody JSONObject jo){
        Integer ssId = jo.getInteger("ssId");
        String cwhname = jo.getString("cwhname");
        String cinvcode = jo.getString("cinvcode");
        String cinvname = jo.getString("cinvname");
        Integer iquantityflag = (jo.getBoolean("iquantityflag") == null || !jo.getBoolean("iquantityflag"))  ? null : 1;
        Integer pdflag = (jo.getBoolean("pdflag") == null || !jo.getBoolean("pdflag"))  ? null : 1;
        Integer czquantityflag = (jo.getBoolean("czquantityflag") == null || !jo.getBoolean("czquantityflag"))  ? null : 1;
        Integer npdflag = (jo.getBoolean("npdflag") == null || !jo.getBoolean("npdflag"))  ? null : 1;
        ErpCurrentStoreVo erpCurrentStoreVo = new ErpCurrentStoreVo(cwhname, cinvcode, cinvname);
        erpCurrentStoreVo.setSsid(ssId);
        erpCurrentStoreVo.setIquantityflag(iquantityflag);
        erpCurrentStoreVo.setPdflag(pdflag);
        erpCurrentStoreVo.setNpdflag(npdflag);
        erpCurrentStoreVo.setCzquantityflag(czquantityflag);
        List<ErpCsSnapshotRecord> erpCsSnapshotRecordList = csSnapshotService.listErpCsSnapshotRecord(erpCurrentStoreVo);
        String fileName = "盘点记录-" + Util.getBusinessNo() + ".xlsx";
        String filePath = config.profile + "/temp/" + fileName;
        csSnapshotService.exportErpCurrentStore(filePath, erpCsSnapshotRecordList);
        return this.success(fileName);
    }

    /*下拉选择仓库的selected数据*/
    @RequestMapping("listrecordwh")
    public String listRecordWh(@RequestBody JSONObject jo){
        Integer ssId = jo.getInteger("ssId");
        List<HashMap<String,Object>> whList = csSnapshotService.listErpCangku(ssId);
        return this.success(whList);
    }
}
